﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlTextBox : SenseControl
    {
        public TextBox TextBox;
        public bool SendButton { get; set; }


        private Point _location;
        private char _passChar;
        private const int _sendButtonSize = 70;
        public Rectangle SendRec { get; private set; }

        public SenseControlTextBox()
        {
            TextBox = new TextBox
            {
                BorderStyle = BorderStyle.None,
                ScrollBars = ScrollBars.Vertical
            };

            SetupStyle();
            ItemType = XFItemType.Clickable;
        }

        public string Text { get; set; }

        public override int GetHeight()
        {
            return ItemRectangle.Height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            int minHeight = this.GetImageRec("textbox").Height;

            var rec = new Rectangle(x, y, GetWidth(), ItemRectangle.Height);
            SendRec = new Rectangle(x + GetWidth() - (_sendButtonSize + _textSpace), y + _textSpace, _sendButtonSize, _sendButtonSize);

            _location = new Point(x + Style.PaddingLeft, y + (Style.PaddingTop >> 1));

            if (minHeight == GetHeight())
                DrawUI("textbox", g, rec);
            else
            {
                Rectangle top = this.GetImageRec("textbox-top");
                Rectangle bottom = this.GetImageRec("textbox-bottom");

                top.Width = rec.Width;
                bottom.Width = rec.Width;

                top.X = rec.X;
                top.Y = rec.Y;
                var middle = new Rectangle(top.X, top.Y + top.Height, rec.Width, rec.Height - (top.Height + bottom.Height));
                bottom.X = rec.X;
                bottom.Y = top.Y + top.Height + middle.Height;

                DrawUI("textbox-top", g, top, false);
                DrawUI("textbox-middle", g, middle, false);
                DrawUI("textbox-bottom", g, bottom, false);
            }

            if (SendButton)
                DrawUI("send", g, SendRec);

            if (string.IsNullOrEmpty(Text)) return;

            int tbWidth = GetWidth() - (Style.PaddingLeft + Style.PaddingRight);
            int tbHeight = GetHeight() - (Style.PaddingTop);

            if (_passChar == '\0')
                g.DrawString(Text, new Rectangle(Style.PaddingLeft + x, Style.PaddingTop + y - 5, tbWidth, tbHeight), Style);
            else
            {
                string pass = Text.Aggregate("", (current, t) => current + (_passChar + ""));
                g.DrawString(pass, new Rectangle(Style.PaddingLeft + x, Style.PaddingTop + y - 5, tbWidth, tbHeight), Style);
            }
        }

        public override void OnFocus()
        {
            base.OnFocus();
            int width = GetWidth();
            if (SendButton)
                width -= _sendButtonSize;

            if (TextBox.InvokeRequired)
            {
                TextBox.Invoke(new Action(() =>
                {
                    TextBox.Width = width - (Style.PaddingLeft + Style.PaddingRight);
                    TextBox.Height = GetHeight() - (Style.PaddingTop);
                    TextBox.Location = _location;
                    _passChar = TextBox.PasswordChar;
                }));
            }
            else
            {
                _passChar = TextBox.PasswordChar;
                TextBox.Width = width - (Style.PaddingLeft + Style.PaddingRight);
                TextBox.Height = GetHeight() - (Style.PaddingTop);
                TextBox.Location = _location;
            }

            if (Parent != null)
            {
                if (Parent is XFPanelList)
                    ((XFPanelList)Parent).MoveToItem(this);

                Parent.Controls.Add(TextBox);
            }
            TextBox.Focus();
        }

        public override void OnBlur()
        {
            base.OnBlur();
            if (Parent != null)
            {
                Parent.Controls.Remove(TextBox);
            }

            Invalidate();
        }

        public override int GetWidth()
        {
            int offset = 0;
            if (Owner != null)
            {
                offset += Owner.Parent.AlphaBarWidth;
            }
            return ItemRectangle.Width - offset;
        }

        private void SetupStyle()
        {
            Style = new XFItemStyle
                    {
                        TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular),
                        TextColor = Color.Black,
                        Padding = 10
                    };

            Style.PropertyChanged += (o, e) => StyleModify();

            StyleModify();
        }

        private void StyleModify()
        {
            int width = GetWidth();

            if (SendButton)
                width -= _sendButtonSize;
            ItemRectangle = this.GetImageRec("textbox");
            int padding = Style.TextFont.MeasureString("text", new Rectangle(0, 0, width, 0)).Height;

            TextBox.Width = width - (Style.PaddingLeft + Style.PaddingRight);
            TextBox.Height = padding - (Style.PaddingTop + Style.PaddingBottom);
            TextBox.Location = new Point(Style.PaddingLeft, Style.PaddingTop);
            TextBox.Font = Style.TextFont;
        }
    }
}